<?php

namespace App\Http\Controllers\Manage;

use App\Common\Traits\Customer\CustomerTrait;
use App\Helpers\ResponseData;
use App\Helpers\ServiceTrait;
use App\Http\Controllers\Controller;
use App\Imports\CustomerImport;
use App\Imports\ShortCut;
use App\Models\CustomerManager;

use App\Models\ShopDetail;
use App\Models\SubAccount\SubAccount;
use Illuminate\Http\Request;
use App\Http\Requests\CustomerManagerRequest;
use Maatwebsite\Excel\Facades\Excel;

class CustomerManageController extends Controller
{
    use ResponseData,ServiceTrait, CustomerTrait;

    public function list(CustomerManager $model,Request $request){
        $user = $request->user();
        if($user->data_permission){
            $model = $this->model($model,$user);
        }
        $size = $request->input('size',10);
        $model = $model->with('person','first','two','three','serviceCategory','shop');
        if($request->filled('start')&&$request->filled('end')){
            $model = $model->whereBetween('created_at',[$request->start,$request->end]);
        }
        if($request->filled('company_name')){
            $model = $model->where('company_name','like','%'.$request->company_name.'%');
        }
        if($request->filled('system_user')){
            $model = $model->where('system_user_name','like','%'.$request->system_user.'%');
        }
        if($request->filled('status')&&$request->status=='all'){
            $data = $model->orderBy('id','desc')->get();
        }else{
            $data = $model->orderBy('id','desc')->paginate($size);
        }
        return $this->success($data);
    }
    //下载模板
    public function downLoadCustomerTemplate(){
        return response()->download('target/客户管理模板.xls');
    }
    // 导入客户
    public function importCustomer(Request $request){
        $res  = Excel::import(new CustomerImport(), $request->file('file'));
        if($res){
            $msg='成功导入 ';
            return $this->success(['msg'=>$msg]);
        }
    }
    public function store(CustomerManager $model,Request $request){

        $data = $request->except('shop');
        $data['department_id'] = $this->getLastDepartment($request);

//        $data['customer_no'] = $this->genCustomerNo();    //20230805：不再自动生成，用id当编号，从1开始

        $model->fill($data);
        $model->save();
        return $this->success('保存成功');
    }
    //生成客户编号
    public function genCustomerNo(){
        $year=date('Y');
        $month=date('m');
        $default=$year.'-'.$month.'-'.'001';
        $last=CustomerManager::orderBy('id','desc')->first();
        //有记录
        if($last){
            //有编号
            if($last->customer_no){
                $noArr=explode('-',$last->customer_no);
                //编号格式正确
                if($noArr && count($noArr)==3){
                    //最后一条的编号是当前本年本月，继续往上加
                    if($noArr[0]==$year && $noArr[1]==$month){
                        $endNo=(int)$noArr[2];
                        $endNo+=1;
                        //变成3位字符串
                        if($endNo<10){
                            $endNo='00'.$endNo;
                        }elseif ($endNo<100){
                            $endNo='0'.$endNo;
                        }
                        return $year.'-'.$month.'-'.$endNo;
                    }
                }
            }
        }
        return $default;
    }

    protected function getArr($request){
        if(is_array($request->shop)){
            return $request->shop;
        }else{
            return json_decode($request->shop,true);
        }
    }
    public function edit(CustomerManager $model,Request $request){
        $data = $model->find($request->id);
        return $this->success($data);
    }

    //
    public function update(CustomerManager $model,Request $request){
        $requestData=$request->except('shop');
        $data = $model->find($request->id);
        if($data && isset($requestData['cooperation_status']) && $requestData['cooperation_status']==2){
            $requestData['breakup_time'] = date('Y-m-d H:i:s');
            $requestData['service_days'] = $this->getServiceDays($data);
            $this->disableAllSubAccounts($data);
        }
        $data->update($requestData);
        return $this->success('修改成功');
    }
    public function getServiceDays($data){
        $timeApart=time()-strtotime($data->created_at);
        return round($timeApart/24/3600);
    }

    public function destory(CustomerManager $model,Request $request){
        $data = $model->find($request->id);
        $data->delete();
    }
}
